<template>
  <span>
    {{ model }}
  </span>
</template>

<script>
import {parseTime} from "@/utils";
import XEUtils from 'xe-utils';
/**
 * 格式化
 * @author maolin
 */
export default {
  name: "ErpViewDate",
  props: {
    /**
     * 值
     * @public
     */
    value: {
      type: [String, Number],
      default: ''
    },
    /**
     * 格式化类型
     * ```js
     * return {
          type: 'amount',
          props: {
            digits: 2
          }
        }
     * ```
     * @public
     */
    formatType: {
      default() {
        return {
          type: 'amount',
          props: {
            digits: 2
          }
        };
      }
    }
  },
  computed: {
    model: function () {
      let formatType = this.formatType;
      let value = this.value;
      if (value === undefined || value === null || value === '') {
        return value;
      }
      if (!formatType || typeof formatType !== 'object') {
        return this.value;
      } else if (formatType.type === 'amount') {
        let digits = formatType.props && formatType.props.digits ? formatType.props.digits : 2;
        return XEUtils.commafy(XEUtils.toNumber(this.value), {digits: digits});
      } else if (formatType.type === 'date') {
        let format = formatType.props && formatType.props.format ? formatType.props.format : '{y}-{m}-{d} {h}:{i}';
        return parseTime(this.value, format);
      }
      return this.value;
    }
  }
};
</script>

<style scoped>

</style>
